package com.doudou.collection.adapter.controller.admin;

import com.doudou.collection.domain.service.admin.LoginService;
import com.doudou.collection.domain.service.admin.SysMenuService;
import com.doudou.collection.domain.service.admin.SysRoleService;
import com.doudou.collection.domain.service.admin.SysUserService;
import com.doudou.collection.infrastructure.common.security.CurrentPrincipal;
import com.doudou.collection.infrastructure.common.security.SecurityUtils;
import com.doudou.collection.infrastructure.common.web.ResultVO;
import com.doudou.collection.orm.dto.admin.login.LoginDTO;
import com.doudou.collection.orm.entity.admin.SysUser;
import com.doudou.collection.orm.vo.UserLoginSimpleVO;
import com.doudou.collection.orm.vo.admin.ImageCodeVO;
import com.doudou.collection.orm.vo.admin.SysMenuListVO;
import com.doudou.collection.orm.vo.admin.UserPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName LoginController
 * @Description 首页
 * @Author blue sky
 * @Date 2023/6/23 14:07
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/dev-api")
@Api(tags = "11.后台管理主页")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("/login")
    @ApiOperation("用户名密码登录功能")
    public ResultVO login(@Validated @RequestBody LoginDTO loginDTO) {
        log.debug("正在处理用户名密码登录请求 ==> {}", loginDTO);
        UserLoginSimpleVO loginUser = loginService.login(loginDTO);
        return ResultVO.ok(loginUser);
    }

    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    @ApiOperation("生成验证码")
    public ResultVO getCode() {
        log.debug("开始处理【获取图片验证码】的请求，无参数");
        ImageCodeVO imageCode = loginService.generateCaptchaImg();
        return ResultVO.ok(imageCode);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    @ApiOperation("获取用户信息")
    public ResultVO getInfo() {
        CurrentPrincipal principal = SecurityUtils.getLoginUser();

        UserPermissionVO user = new UserPermissionVO();
        SysUser sysUser = sysUserService.getByUsername(principal.getUsername());
        List<String> roles = sysRoleService.getRolePermission(principal);
        List<String> permissions = sysRoleService.getMenuPermission(principal);

        user.setUser(sysUser);
        user.setRoles(roles);
        user.setPermissions(permissions);


        return ResultVO.ok(user);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/getRouters")
    @ApiOperation("获取用户对应的菜单路由")
    public ResultVO getRouters() {
        log.debug("正在处理【获取用户对应菜单路由】请求，无参数");
        Long userId = SecurityUtils.getUserId();
        List<SysMenuListVO> list = sysMenuService.listMenuTreeByUserId(userId);
        return ResultVO.ok(sysMenuService.buildMenus(list));
    }
}













